package com.climber.android.commonsdk.app

import android.content.Context
import android.net.Uri
import android.os.Bundle
import com.alibaba.android.arouter.launcher.ARouter
import com.climber.android.commonsdk.R
import com.climber.android.commonsdk.arouter.ARouterPathConstants
import io.ganguo.library.mvp.arouter.ARouterUtils
import io.ganguo.library.mvp.arouter.BundleHelper
import io.ganguo.library.mvp.util.ActivityUtils
import okhttp3.HttpUrl

/**
 * Created by Mikes at 2020-04-09 09:20
 */
object AppDeepLink {
    /**
     * DeepLink Scheme & Host
     */
    private const val DEEP_LINK_SCHEME = "climber"
    private const val DEEP_LINK_HOST = "mobile"

    private const val DEEP_LINK_AUTHORITY = "$DEEP_LINK_SCHEME://$DEEP_LINK_HOST"

    fun makeDeepLinkUri(path : String) : Uri = Uri.parse(DEEP_LINK_AUTHORITY + path)

    fun checkUrlMatchDeepLinkScheme(url : String?) : Boolean = url?.startsWith(DEEP_LINK_AUTHORITY) ?: false

    fun parseDeepLink(context: Context?=null, link: String?,
                      animIn : Int = R.anim.cbase__slide_in_from_right,
                      animOut : Int = R.anim.cbase__slide_out_to_left,
                      completion: ((matched: Boolean) -> Unit)): Boolean {
        try {
            if (link == null || link.isEmpty()) {
                completion(false)
                return false
            }

            val deepLinkUri = Uri.parse(link)

            //`lifanou://mobile/xxx/xxx`
            if (checkUrlMatchDeepLinkScheme(link)) {
                val linkPath = deepLinkUri.path
                return if(linkPath != null){
                    ARouter.getInstance().build(deepLinkUri)
                            .withTransition(animIn, animOut)
                            .navigation(ActivityUtils.getTopActivity())
                    true
                } else{
                    false
                }
            }
            //`https://xxxx` or `http://xxxx`
            else if (HttpUrl.defaultPort(deepLinkUri.scheme ?: "") > 0) {
                ARouter.getInstance().build(ARouterPathConstants.PATH_WEB_BROWSER)
                        .withTransition(animIn, animOut)
                        .with(BundleHelper().putString(AppConstants.PARAM_WEB_URL, link).build())
                        .navigation(ActivityUtils.getTopActivity())
                completion(true)
                return true
            }
            //others, not support
            else {
                completion(false)
                return false
            }
        } catch (e: Exception) {
            completion(false)
            return false
        }
    }

    /**
     * 打开任务链接
     */
    fun openTaskLinkUrl(link: String?,
                        taskId : String,
                        animIn : Int = R.anim.cbase__slide_in_from_right,
                        animOut : Int = R.anim.cbase__slide_out_to_left,
                        completion: ((matched: Boolean) -> Unit)){
        if (link == null || link.isEmpty()) {
            completion(false)
            return
        }

        val deepLinkUri = Uri.parse(link)

        //`lifanou://mobile/xxx/xxx`
        if (checkUrlMatchDeepLinkScheme(link)) {
            val linkPath = deepLinkUri.path
            if(linkPath != null){
                ARouter.getInstance().build(deepLinkUri)
                        .withTransition(animIn, animOut)
                        .with(Bundle().apply { putString(AppConstants.PARAM_TASK_ID, taskId) })
                        .navigation(ActivityUtils.getTopActivity())
                completion(true)
            } else{
                completion(false)
            }
        }
        //others, not support
        else {
            completion(false)
        }
    }
}